/**
 * @author htl
 * @date 2018/11/14 0014
 * @Description:
 */
const mongoose = require('mongoose');
const bookSchema = new mongoose.Schema({
    kind: {    //书种类，电子书 1   纸质书2
        type: Number,
        default: 1
    },
    firstSortId: {   //一级分类主键
        type: String,
    },
    secondSortId: {   //二级分类主键
        type: String,
    },
    bookName: {   //书名
        type: String,
        trim: true,
        default: '',
        required: true,
        maxlength: 30,
    },
    description: {      //简介
        type: String,
        default: ''
    },
    bookIcon: {   //书图标
        type: String,
        default: ''
    },
    author: {      //作者
        type: String,
        default: ''
    },
    publish: {  //出版
        type: String,
        default: ''
    },
    costPrice: {   //原价
        type: Number,
        default: ''
    },
    price: {   //销售价
        type: Number,
        default: ''
    },
    hot: {   //是否为热卖商品
        type: Boolean,
        default: false
    },
    salesCount: {   //销量统计
        type: Number,
        default: ''
    }

},{timestamps: {createdAt: 'createTime', updatedAt: 'upDateTime'}})

//获取数据总条数
bookSchema.statics.getCount = function(cb) {
    this.count((err,num) => {
        if(err) {
            return new Error(err);
        }
        cb(num)
    })
}

const bookModel = mongoose.model('books',bookSchema); //创建集合（表）


module.exports = bookModel;